﻿using EentitfFrameworkMvc.Models;
using Microsoft.AspNetCore.Mvc;

namespace EentitfFrameworkMvc.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StuController : Controller
    {
        private readonly Data01Context _context;

        public StuController(Data01Context context)
        {
            _context = context;
        }
        /// <summary>
        /// 根据姓名进行模糊查询，keyword
        /// </summary>
        /// <param name="KeyWord"></param>
        /// <returns></returns>
        [HttpGet]
        // GET: api/Stu/SelectByKeyWord
        public List<TbStu> SelectByKeyWord(String name)
        {
            //Data01Context context = new Data01Context();
            //查询所有的数据.返回数组集合
            var data = _context.TbStu.ToList();
            //如果KeyWord不为空的话，根据条件进行模糊查询
            if (!string.IsNullOrEmpty(name))
            {
                data = data.Where(s => s.StuName!.Contains(name)).ToList();
            }
            return data;
        }

        /// <summary>
        /// 查询全部，
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        // GET: api/Stu/SelectAll
        //返回TbStu实体类数组集合
        public List<TbStu> SelectAll()
        {
            var data = _context.TbStu.ToList();
            return data;
        }

        /// <summary>
        /// 修改操作，先根据Id查询出来数据，再根据该数据的id进行数据的修改
        /// </summary>
        /// <param name="stu"></param>
        [HttpPost]
        // Post: api/Stu/UpdateById
        public void UpdateById(TbStu stu)
        {
            var data = _context.TbStu.Where(x => x.StuId == stu.StuId).FirstOrDefault();
            //把我们新的值，赋值到老的这条数据上
            data.StuName = stu.StuName;
            data.StuSpwd = stu.StuSpwd;
            data.StuSex = stu.StuSex;
            data.StuAge = stu.StuAge;
            data.StuIdentity = stu.StuIdentity;
            data.StuHome = stu.StuHome;
            data.StuEmail = stu.StuEmail;
            data.StuStatus = stu.StuStatus;
            //最后做一个保存数据的操作
            _context.SaveChanges();
        }

        /// <summary>
        /// 删除操作，根据id查询该数据是否存在，若存在则返回该数据的id进行删除
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        [HttpPost]
        // Post: api/Stu/DeleteById
        public bool DeleteById(TbStu stu)
        {
            var data = _context.TbStu.Where(x => x.StuId == stu.StuId).FirstOrDefault();
            //所查询的数据不为空时执行删除操作
            if (data != null)
            {
                _context.TbStu.Remove(data);
                _context.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 数据新增，由于该对象的id是自增的，因此从前端返回除了id之外的Json数据即可
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        [HttpPost]
        // Post: api/Stu/SaveAdd
        public bool SaveAdd(TbStu stu)
        {
            _context.Add(stu);
            var count = _context.SaveChanges();
            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
